<?php
namespace SB\File;
class Upload
{
	public $root;
	public $upload;
	public $table;

	function __construct($root,$path,$table = 'file'){
		$this->root = $root;
		$this->upload = $path;
		$this->table = $table;
	}

	function all($file){
		// if ((($_FILES["file"]["type"] == "image/gif")
		// || ($_FILES["file"]["type"] == "image/jpeg")
		// || ($_FILES["file"]["type"] == "image/pjpeg"))
		// && ($_FILES["file"]["size"] < 20000))
		// print_r($_FILES);
		if ($_FILES[$file]["error"] > 0)
		{
			return false;
			echo "Error: " . $_FILES[$file]["error"] . "<br />";
		}
		else
		{
			// echo "Upload: " . $_FILES[$file]["name"] . "<br />";
			// echo "Type: " . $_FILES[$file]["type"] . "<br />";
			// echo "Size: " . ($_FILES[$file]["size"] / 1024) . " Kb<br />";
			// echo "Stored in: " . $_FILES[$file]["tmp_name"];
			$saveName = $this->makePath();
			$MUF = move_uploaded_file( $_FILES[$file]["tmp_name"] , $this->root . $saveName);
			if($MUF){
				return $this->addFile( $_FILES[$file], $saveName);
			} else {
				return false;
			}
			
		}
	}

	function makeName(){
		return date("YmdHis") . rand(1,999);
	}

	function makePath(){
		return $this->upload . $this->makeName();
	}


	function addFile( $file, $saveName){
		$db = \think\Db::name( $this->table );
		$map['fileName'] = $file['name'];
		$map['fileType'] = $file['type'];
		$map['fileSize'] = $file['size'];
		$map['saveName'] = $saveName;
		$id = $db->insertGetId($map);
		return $id;
	}
}